package com.woniu.bank.service.impl;


import com.woniu.bank.dto.PermissionDTO;
import com.woniu.bank.entity.SysPermission;
import com.woniu.bank.mapper.SysPermissionMapper;
import com.woniu.bank.service.SysPermissionService;
import com.woniu.bank.vo.TreeNodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：lgy
 * @description：生成登陆对象的所有权限集合
 * @date ：2022/12/6 12:04
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public List<SysPermission> selectPermissionByTel(String tel, Integer pid) {
        List<SysPermission> sysPermissions = sysPermissionMapper.selectPermissionByTel(tel, pid);
        for (SysPermission sysPermission : sysPermissions) {
            List<SysPermission> sysPermissions1 = selectPermissionByTel(tel, sysPermission.getId());
            sysPermission.setChildren(sysPermissions1);
        }
        return sysPermissions;
    }

    @Override
    public List<TreeNodeVO> getPermissionByParentId(Integer pid) {
        List<TreeNodeVO> permissions = sysPermissionMapper.getPermissionByParentId(pid);
        if (permissions != null && permissions.size() > 0) {
            for (TreeNodeVO premission : permissions) {
                List<TreeNodeVO> subPermissions = getPermissionByParentId(premission.getId());
                premission.setChildren(subPermissions);
            }
        }
        return permissions;
    }

    @Override
    @Transactional
    public void updatePermissionByRid(PermissionDTO permissionDTO) {
        sysPermissionMapper.deleteByRid(permissionDTO.getRid());
        sysPermissionMapper.insertRolePermission(permissionDTO);
    }
}
